{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CaDRReS-Sc for predicting combinatorial drug response\n",
    "This notebook explains how load a pre-trained CaDRReS-Sc model and predict drug response combination for your own data. In this example, we used scRNA-seq data of patient-derived cell lines obtained from head and neck patients. Cell clustering result is based on Scanpy package. For the detail of data preprocessing, cell clustering, and drug response prediction, please refer to our manuscript."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:37.566935Z",
     "start_time": "2020-11-27T08:11:35.858802Z"
    }
   },
   "outputs": [],
   "source": [
    "import sys, os, pickle\n",
    "from collections import Counter\n",
    "import importlib\n",
    "from ipywidgets import widgets\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "scriptpath = '..'\n",
    "sys.path.append(os.path.abspath(scriptpath))\n",
    "pd.set_option('precision', 2)\n",
    "\n",
    "from cadrres_sc import pp, model, evaluation, utility"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load pre-trained model\n",
    "We will load a model and make a prediction based on HNSC pretrained model. Alternatively, a model trained previously in [notebook_02_model_prediction.ipynb](./notebook_02_model_prediction.ipynb) can also be used."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load the pre-trained model based on your selection\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:37.583031Z",
     "start_time": "2020-11-27T08:11:37.568326Z"
    }
   },
   "outputs": [],
   "source": [
    "model_dir = '../data/pretrained_model/'\n",
    "model_name = 'hn_drug_cw_dw10_100000'\n",
    "model_file = model_dir + '{}_param_dict.pickle'.format(model_name)\n",
    "\n",
    "cadrres_model = model.load_model(model_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Read test data\n",
    "Again, for this example we load GDSC dataset.\n",
    "Note: GDSC_exp.tsv can be downloaded from https://www.dropbox.com/s/3v576mspw5yewbm/GDSC_exp.tsv?dl=0\n",
    "\n",
    "## Notes for other test data\n",
    "\n",
    "You can apply the model to other gene expression dataset. The input gene expression matrix should have been normalized, i.e. **for each sample, expression values are comparable across genes**. \n",
    "\n",
    "In this example the gene expression matrix provided by GDSC is already normalized using RMA.\n",
    "\n",
    "For RNA-seq data, read count should be normalized by gene length, using normalization methods such as TPM."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:40.650694Z",
     "start_time": "2020-11-27T08:11:37.584611Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dataframe shape: (17419, 1018) \n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>906826</th>\n",
       "      <th>687983</th>\n",
       "      <th>910927</th>\n",
       "      <th>1240138</th>\n",
       "      <th>1240139</th>\n",
       "      <th>906792</th>\n",
       "      <th>910688</th>\n",
       "      <th>1240135</th>\n",
       "      <th>1290812</th>\n",
       "      <th>907045</th>\n",
       "      <th>...</th>\n",
       "      <th>753584</th>\n",
       "      <th>907044</th>\n",
       "      <th>998184</th>\n",
       "      <th>908145</th>\n",
       "      <th>1659787</th>\n",
       "      <th>1298157</th>\n",
       "      <th>1480372</th>\n",
       "      <th>1298533</th>\n",
       "      <th>930299</th>\n",
       "      <th>905954.1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GENE</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A1BG</th>\n",
       "      <td>6.21</td>\n",
       "      <td>5.03</td>\n",
       "      <td>5.51</td>\n",
       "      <td>4.21</td>\n",
       "      <td>3.40</td>\n",
       "      <td>4.92</td>\n",
       "      <td>3.83</td>\n",
       "      <td>5.15</td>\n",
       "      <td>3.11</td>\n",
       "      <td>5.06</td>\n",
       "      <td>...</td>\n",
       "      <td>4.27</td>\n",
       "      <td>3.44</td>\n",
       "      <td>4.93</td>\n",
       "      <td>2.90</td>\n",
       "      <td>4.52</td>\n",
       "      <td>5.07</td>\n",
       "      <td>2.96</td>\n",
       "      <td>3.09</td>\n",
       "      <td>4.05</td>\n",
       "      <td>5.33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A1CF</th>\n",
       "      <td>2.98</td>\n",
       "      <td>2.95</td>\n",
       "      <td>2.87</td>\n",
       "      <td>3.08</td>\n",
       "      <td>2.85</td>\n",
       "      <td>3.22</td>\n",
       "      <td>3.00</td>\n",
       "      <td>2.89</td>\n",
       "      <td>2.76</td>\n",
       "      <td>2.99</td>\n",
       "      <td>...</td>\n",
       "      <td>2.94</td>\n",
       "      <td>3.16</td>\n",
       "      <td>2.98</td>\n",
       "      <td>3.12</td>\n",
       "      <td>2.98</td>\n",
       "      <td>2.91</td>\n",
       "      <td>2.94</td>\n",
       "      <td>2.78</td>\n",
       "      <td>2.87</td>\n",
       "      <td>2.93</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 1018 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      906826  687983  910927  1240138  1240139  906792  910688  1240135  \\\n",
       "GENE                                                                      \n",
       "A1BG    6.21    5.03    5.51     4.21     3.40    4.92    3.83     5.15   \n",
       "A1CF    2.98    2.95    2.87     3.08     2.85    3.22    3.00     2.89   \n",
       "\n",
       "      1290812  907045  ...  753584  907044  998184  908145  1659787  1298157  \\\n",
       "GENE                   ...                                                     \n",
       "A1BG     3.11    5.06  ...    4.27    3.44    4.93    2.90     4.52     5.07   \n",
       "A1CF     2.76    2.99  ...    2.94    3.16    2.98    3.12     2.98     2.91   \n",
       "\n",
       "      1480372  1298533  930299  905954.1  \n",
       "GENE                                      \n",
       "A1BG     2.96     3.09    4.05      5.33  \n",
       "A1CF     2.94     2.78    2.87      2.93  \n",
       "\n",
       "[2 rows x 1018 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gene_exp_df = pd.read_csv('../data/GDSC/GDSC_exp.tsv', sep='\\t', index_col=0)\n",
    "gene_exp_df = gene_exp_df.groupby(gene_exp_df.index).mean()\n",
    "print(\"Dataframe shape:\", gene_exp_df.shape, \"\\n\")\n",
    "gene_exp_df.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculate fold-change\n",
    "We normalized baseline gene expression values for each gene by computing fold-changes compared to the median value across cell-lines"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:44.506253Z",
     "start_time": "2020-11-27T08:11:40.652448Z"
    }
   },
   "outputs": [],
   "source": [
    "cell_line_log2_mean_fc_exp_df, cell_line_mean_exp_df = pp.gexp.normalize_log2_mean_fc(gene_exp_df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load cluster-specific gene expression profile\n",
    "Dimensions: genes x clusters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:44.509925Z",
     "start_time": "2020-11-27T08:11:44.507611Z"
    }
   },
   "outputs": [],
   "source": [
    "cluster_norm_exp_fname = '../data/patient/log2_fc_cluster_tpm.csv'\n",
    "output_dir = '../example_result/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:44.595874Z",
     "start_time": "2020-11-27T08:11:44.511340Z"
    }
   },
   "outputs": [],
   "source": [
    "cluster_norm_exp_df = pd.read_csv(cluster_norm_exp_fname, index_col=0).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:44.612425Z",
     "start_time": "2020-11-27T08:11:44.597031Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>cluster</th>\n",
       "      <th>A1</th>\n",
       "      <th>A2</th>\n",
       "      <th>B1</th>\n",
       "      <th>B2</th>\n",
       "      <th>C1</th>\n",
       "      <th>C2</th>\n",
       "      <th>D1</th>\n",
       "      <th>D2</th>\n",
       "      <th>E1</th>\n",
       "      <th>E2</th>\n",
       "      <th>...</th>\n",
       "      <th>G1</th>\n",
       "      <th>G2</th>\n",
       "      <th>H1</th>\n",
       "      <th>I1</th>\n",
       "      <th>I2</th>\n",
       "      <th>J1</th>\n",
       "      <th>J2</th>\n",
       "      <th>K1</th>\n",
       "      <th>L</th>\n",
       "      <th>M</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>AAAS</th>\n",
       "      <td>0.34</td>\n",
       "      <td>0.73</td>\n",
       "      <td>-0.10</td>\n",
       "      <td>0.04</td>\n",
       "      <td>-0.75</td>\n",
       "      <td>0.44</td>\n",
       "      <td>0.97</td>\n",
       "      <td>0.65</td>\n",
       "      <td>0.24</td>\n",
       "      <td>-0.11</td>\n",
       "      <td>...</td>\n",
       "      <td>0.02</td>\n",
       "      <td>0.14</td>\n",
       "      <td>0.93</td>\n",
       "      <td>-0.50</td>\n",
       "      <td>-0.56</td>\n",
       "      <td>-0.23</td>\n",
       "      <td>-2.15</td>\n",
       "      <td>-0.67</td>\n",
       "      <td>-1.38</td>\n",
       "      <td>0.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AAMP</th>\n",
       "      <td>0.47</td>\n",
       "      <td>0.81</td>\n",
       "      <td>-0.54</td>\n",
       "      <td>-0.89</td>\n",
       "      <td>-1.26</td>\n",
       "      <td>0.21</td>\n",
       "      <td>0.20</td>\n",
       "      <td>-0.08</td>\n",
       "      <td>0.64</td>\n",
       "      <td>-0.55</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.30</td>\n",
       "      <td>-0.57</td>\n",
       "      <td>0.71</td>\n",
       "      <td>-0.94</td>\n",
       "      <td>-0.96</td>\n",
       "      <td>-0.85</td>\n",
       "      <td>-1.17</td>\n",
       "      <td>-1.75</td>\n",
       "      <td>-1.93</td>\n",
       "      <td>0.72</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "cluster    A1    A2    B1    B2    C1    C2    D1    D2    E1    E2  ...  \\\n",
       "AAAS     0.34  0.73 -0.10  0.04 -0.75  0.44  0.97  0.65  0.24 -0.11  ...   \n",
       "AAMP     0.47  0.81 -0.54 -0.89 -1.26  0.21  0.20 -0.08  0.64 -0.55  ...   \n",
       "\n",
       "cluster    G1    G2    H1    I1    I2    J1    J2    K1     L     M  \n",
       "AAAS     0.02  0.14  0.93 -0.50 -0.56 -0.23 -2.15 -0.67 -1.38  0.46  \n",
       "AAMP    -0.30 -0.57  0.71 -0.94 -0.96 -0.85 -1.17 -1.75 -1.93  0.72  \n",
       "\n",
       "[2 rows x 24 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cluster_norm_exp_df.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Read essential genes list\n",
    "\n",
    "Or in case you want your training using one specific set of genes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:44.618206Z",
     "start_time": "2020-11-27T08:11:44.613744Z"
    }
   },
   "outputs": [],
   "source": [
    "ess_gene_list = utility.get_gene_list('../data/essential_genes.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:44.624557Z",
     "start_time": "2020-11-27T08:11:44.620361Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1724"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "selected_gene_list = [g for g in ess_gene_list if g in cluster_norm_exp_df.index]\n",
    "len(selected_gene_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculate kernel feature \n",
    "\n",
    "Now we will compute the kernel features between your loaded dataset (e.g, clustered patient data) and cell lines (or any other model you trained previously, e.g. CaDRReS-SC trained on PDX data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.246141Z",
     "start_time": "2020-11-27T08:11:44.626219Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calculating kernel features based on 1543 common genes\n",
      "(1724, 24) (17419, 1018)\n"
     ]
    }
   ],
   "source": [
    "test_kernel_df = pp.gexp.calculate_kernel_feature(cluster_norm_exp_df, cell_line_log2_mean_fc_exp_df, selected_gene_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.265073Z",
     "start_time": "2020-11-27T08:11:46.247445Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dataframe shape: (24, 1018) \n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>906826</th>\n",
       "      <th>687983</th>\n",
       "      <th>910927</th>\n",
       "      <th>1240138</th>\n",
       "      <th>1240139</th>\n",
       "      <th>906792</th>\n",
       "      <th>910688</th>\n",
       "      <th>1240135</th>\n",
       "      <th>1290812</th>\n",
       "      <th>907045</th>\n",
       "      <th>...</th>\n",
       "      <th>753584</th>\n",
       "      <th>907044</th>\n",
       "      <th>998184</th>\n",
       "      <th>908145</th>\n",
       "      <th>1659787</th>\n",
       "      <th>1298157</th>\n",
       "      <th>1480372</th>\n",
       "      <th>1298533</th>\n",
       "      <th>930299</th>\n",
       "      <th>905954.1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A1</th>\n",
       "      <td>-0.09</td>\n",
       "      <td>6.08e-03</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.05</td>\n",
       "      <td>-0.02</td>\n",
       "      <td>-0.03</td>\n",
       "      <td>-0.02</td>\n",
       "      <td>-0.03</td>\n",
       "      <td>-0.01</td>\n",
       "      <td>0.01</td>\n",
       "      <td>...</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.04</td>\n",
       "      <td>0.09</td>\n",
       "      <td>-0.06</td>\n",
       "      <td>3.44e-02</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.06</td>\n",
       "      <td>0.05</td>\n",
       "      <td>-0.10</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A2</th>\n",
       "      <td>-0.09</td>\n",
       "      <td>2.55e-02</td>\n",
       "      <td>0.02</td>\n",
       "      <td>0.02</td>\n",
       "      <td>-0.03</td>\n",
       "      <td>-0.02</td>\n",
       "      <td>-0.11</td>\n",
       "      <td>-0.04</td>\n",
       "      <td>-0.06</td>\n",
       "      <td>-0.06</td>\n",
       "      <td>...</td>\n",
       "      <td>0.07</td>\n",
       "      <td>0.09</td>\n",
       "      <td>0.11</td>\n",
       "      <td>-0.15</td>\n",
       "      <td>5.79e-03</td>\n",
       "      <td>0.06</td>\n",
       "      <td>0.09</td>\n",
       "      <td>0.10</td>\n",
       "      <td>-0.09</td>\n",
       "      <td>-0.03</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 1018 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    906826    687983  910927  1240138  1240139  906792  910688  1240135  \\\n",
       "A1   -0.09  6.08e-03    0.01     0.05    -0.02   -0.03   -0.02    -0.03   \n",
       "A2   -0.09  2.55e-02    0.02     0.02    -0.03   -0.02   -0.11    -0.04   \n",
       "\n",
       "    1290812  907045  ...  753584  907044  998184  908145   1659787  1298157  \\\n",
       "A1    -0.01    0.01  ...    0.05    0.04    0.09   -0.06  3.44e-02     0.03   \n",
       "A2    -0.06   -0.06  ...    0.07    0.09    0.11   -0.15  5.79e-03     0.06   \n",
       "\n",
       "    1480372  1298533  930299  905954.1  \n",
       "A1     0.06     0.05   -0.10      0.02  \n",
       "A2     0.09     0.10   -0.09     -0.03  \n",
       "\n",
       "[2 rows x 1018 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"Dataframe shape:\", test_kernel_df.shape, \"\\n\")\n",
    "test_kernel_df.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Drug response prediction\n",
    "Given the pre-trained model we will predict now for your clustered data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.273469Z",
     "start_time": "2020-11-27T08:11:46.266695Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Predicting drug response using CaDRReS: hn_drug_cw_dw10_100000\n",
      "done!\n"
     ]
    }
   ],
   "source": [
    "print('Predicting drug response using CaDRReS: {}'.format(model_name))\n",
    "pred_df, P_test_df= model.predict_from_model(cadrres_model, test_kernel_df)\n",
    "print('done!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Inspecting the model predictions and save the predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.291521Z",
     "start_time": "2020-11-27T08:11:46.275031Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Drug ID</th>\n",
       "      <th>1001</th>\n",
       "      <th>1003</th>\n",
       "      <th>1004</th>\n",
       "      <th>1006</th>\n",
       "      <th>1007</th>\n",
       "      <th>1010</th>\n",
       "      <th>1012</th>\n",
       "      <th>1014</th>\n",
       "      <th>1015</th>\n",
       "      <th>1016</th>\n",
       "      <th>...</th>\n",
       "      <th>299</th>\n",
       "      <th>301</th>\n",
       "      <th>302</th>\n",
       "      <th>303</th>\n",
       "      <th>305</th>\n",
       "      <th>306</th>\n",
       "      <th>308</th>\n",
       "      <th>328</th>\n",
       "      <th>331</th>\n",
       "      <th>346</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A1</th>\n",
       "      <td>10.40</td>\n",
       "      <td>-4.98</td>\n",
       "      <td>-6.04</td>\n",
       "      <td>0.98</td>\n",
       "      <td>-6.94</td>\n",
       "      <td>-0.01</td>\n",
       "      <td>7.38e-01</td>\n",
       "      <td>2.65</td>\n",
       "      <td>3.53</td>\n",
       "      <td>-1.85</td>\n",
       "      <td>...</td>\n",
       "      <td>0.57</td>\n",
       "      <td>2.48</td>\n",
       "      <td>0.21</td>\n",
       "      <td>3.19</td>\n",
       "      <td>3.69</td>\n",
       "      <td>2.49</td>\n",
       "      <td>-0.70</td>\n",
       "      <td>-1.43</td>\n",
       "      <td>1.61</td>\n",
       "      <td>-3.96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A2</th>\n",
       "      <td>9.96</td>\n",
       "      <td>-6.81</td>\n",
       "      <td>-7.07</td>\n",
       "      <td>-0.29</td>\n",
       "      <td>-7.48</td>\n",
       "      <td>0.02</td>\n",
       "      <td>3.01e-03</td>\n",
       "      <td>2.13</td>\n",
       "      <td>2.97</td>\n",
       "      <td>-2.29</td>\n",
       "      <td>...</td>\n",
       "      <td>0.53</td>\n",
       "      <td>2.16</td>\n",
       "      <td>0.41</td>\n",
       "      <td>2.87</td>\n",
       "      <td>3.22</td>\n",
       "      <td>2.27</td>\n",
       "      <td>-1.16</td>\n",
       "      <td>-2.39</td>\n",
       "      <td>1.40</td>\n",
       "      <td>-4.56</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 81 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "Drug ID   1001  1003  1004  1006  1007  1010      1012  1014  1015  1016  ...  \\\n",
       "A1       10.40 -4.98 -6.04  0.98 -6.94 -0.01  7.38e-01  2.65  3.53 -1.85  ...   \n",
       "A2        9.96 -6.81 -7.07 -0.29 -7.48  0.02  3.01e-03  2.13  2.97 -2.29  ...   \n",
       "\n",
       "Drug ID   299   301   302   303   305   306   308   328   331   346  \n",
       "A1       0.57  2.48  0.21  3.19  3.69  2.49 -0.70 -1.43  1.61 -3.96  \n",
       "A2       0.53  2.16  0.41  2.87  3.22  2.27 -1.16 -2.39  1.40 -4.56  \n",
       "\n",
       "[2 rows x 81 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#cluster vs drugs\n",
    "pred_df.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.302015Z",
     "start_time": "2020-11-27T08:11:46.293183Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Saving ../data/pretrained_model/hn_drug_cw_dw10_100000_test_pred.csv\n"
     ]
    }
   ],
   "source": [
    "print('Saving ' + model_dir + '{}_test_pred.csv'.format(model_name))\n",
    "pred_df.to_csv(output_dir + '{}_test_pred.csv'.format(model_name))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Predicting overall drug response and cell death percentage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.307048Z",
     "start_time": "2020-11-27T08:11:46.303564Z"
    }
   },
   "outputs": [],
   "source": [
    "# for each patient, if cell cluster is less than 5%, then we don't consider that cluster \n",
    "freq_cutoff = 0.05\n",
    "# estimate cell death percentage based on log2 of the median IC50 observed in HNSC cell lines (GDSC)\n",
    "ref_type = 'log2_median_ic50_hn'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Read drug statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.316835Z",
     "start_time": "2020-11-27T08:11:46.308683Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(81, 27)\n"
     ]
    }
   ],
   "source": [
    "drug_info_df = pd.read_csv('../preprocessed_data/GDSC/hn_drug_stat.csv', index_col=0)\n",
    "drug_info_df.index = drug_info_df.index.astype(str)\n",
    "\n",
    "drug_id_name_dict = dict(zip(drug_info_df.index, drug_info_df['Drug Name']))\n",
    "print (drug_info_df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.336168Z",
     "start_time": "2020-11-27T08:11:46.318014Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Drug Name</th>\n",
       "      <th>Synonyms</th>\n",
       "      <th>Target</th>\n",
       "      <th>Target Pathway</th>\n",
       "      <th>Selleckchem Cat#</th>\n",
       "      <th>CAS number</th>\n",
       "      <th>PubCHEM</th>\n",
       "      <th>Others</th>\n",
       "      <th>entropy</th>\n",
       "      <th>max_conc</th>\n",
       "      <th>...</th>\n",
       "      <th>median_ic50_9f</th>\n",
       "      <th>log2_median_ic50_9f</th>\n",
       "      <th>log2_median_ic50_hn</th>\n",
       "      <th>median_ic50_hn</th>\n",
       "      <th>median_ic50_3f_hn</th>\n",
       "      <th>log2_median_ic50_3f_hn</th>\n",
       "      <th>median_ic50_9f_hn</th>\n",
       "      <th>log2_median_ic50_9f_hn</th>\n",
       "      <th>num_sensitive</th>\n",
       "      <th>num_sensitive_hn</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Drug ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1001</th>\n",
       "      <td>AICA Ribonucleotide</td>\n",
       "      <td>AICAR, N1-(b-D-Ribofuranosyl)-5-aminoimidazole...</td>\n",
       "      <td>AMPK agonist</td>\n",
       "      <td>Metabolism</td>\n",
       "      <td>S1802</td>\n",
       "      <td>2627-69-2</td>\n",
       "      <td>65110</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.03</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2.07e+02</td>\n",
       "      <td>7.69</td>\n",
       "      <td>9.94</td>\n",
       "      <td>9.82e+02</td>\n",
       "      <td>3.27e+02</td>\n",
       "      <td>8.35</td>\n",
       "      <td>1.09e+02</td>\n",
       "      <td>6.77</td>\n",
       "      <td>476</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1003</th>\n",
       "      <td>Camptothecin</td>\n",
       "      <td>7-Ethyl-10-Hydroxy-Camptothecin, SN-38, Irinot...</td>\n",
       "      <td>TOP1</td>\n",
       "      <td>DNA replication</td>\n",
       "      <td>S1288</td>\n",
       "      <td>7689-03-4</td>\n",
       "      <td>104842</td>\n",
       "      <td>(SN-38, S4908, 86639-52-3) (Irinotecan, S1198,...</td>\n",
       "      <td>4.61</td>\n",
       "      <td>0.1</td>\n",
       "      <td>...</td>\n",
       "      <td>2.00e-03</td>\n",
       "      <td>-8.96</td>\n",
       "      <td>-7.59</td>\n",
       "      <td>5.20e-03</td>\n",
       "      <td>1.73e-03</td>\n",
       "      <td>-9.17</td>\n",
       "      <td>5.78e-04</td>\n",
       "      <td>-10.76</td>\n",
       "      <td>688</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                   Drug Name  \\\n",
       "Drug ID                        \n",
       "1001     AICA Ribonucleotide   \n",
       "1003            Camptothecin   \n",
       "\n",
       "                                                  Synonyms        Target  \\\n",
       "Drug ID                                                                    \n",
       "1001     AICAR, N1-(b-D-Ribofuranosyl)-5-aminoimidazole...  AMPK agonist   \n",
       "1003     7-Ethyl-10-Hydroxy-Camptothecin, SN-38, Irinot...          TOP1   \n",
       "\n",
       "          Target Pathway Selleckchem Cat# CAS number PubCHEM  \\\n",
       "Drug ID                                                        \n",
       "1001          Metabolism            S1802  2627-69-2   65110   \n",
       "1003     DNA replication            S1288  7689-03-4  104842   \n",
       "\n",
       "                                                    Others  entropy  max_conc  \\\n",
       "Drug ID                                                                         \n",
       "1001                                                   NaN     6.03    2000.0   \n",
       "1003     (SN-38, S4908, 86639-52-3) (Irinotecan, S1198,...     4.61       0.1   \n",
       "\n",
       "         ...  median_ic50_9f  log2_median_ic50_9f  log2_median_ic50_hn  \\\n",
       "Drug ID  ...                                                             \n",
       "1001     ...        2.07e+02                 7.69                 9.94   \n",
       "1003     ...        2.00e-03                -8.96                -7.59   \n",
       "\n",
       "         median_ic50_hn  median_ic50_3f_hn  log2_median_ic50_3f_hn  \\\n",
       "Drug ID                                                              \n",
       "1001           9.82e+02           3.27e+02                    8.35   \n",
       "1003           5.20e-03           1.73e-03                   -9.17   \n",
       "\n",
       "         median_ic50_9f_hn  log2_median_ic50_9f_hn  num_sensitive  \\\n",
       "Drug ID                                                             \n",
       "1001              1.09e+02                    6.77            476   \n",
       "1003              5.78e-04                  -10.76            688   \n",
       "\n",
       "         num_sensitive_hn  \n",
       "Drug ID                    \n",
       "1001                   27  \n",
       "1003                   30  \n",
       "\n",
       "[2 rows x 27 columns]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_info_df.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load cluster-specific drug response prediction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.345496Z",
     "start_time": "2020-11-27T08:11:46.337496Z"
    }
   },
   "outputs": [],
   "source": [
    "cadrres_cluster_df = pd.read_csv(output_dir + '{}_test_pred.csv'.format(model_name), index_col=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.351062Z",
     "start_time": "2020-11-27T08:11:46.346647Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "81 24\n"
     ]
    }
   ],
   "source": [
    "#load prediction for a certain set of drugs\n",
    "drug_list = drug_info_df.index\n",
    "cluster_list = cadrres_cluster_df.index\n",
    "print(len(drug_list), len(cluster_list))\n",
    "\n",
    "drug_info_df = drug_info_df.loc[drug_list]\n",
    "cadrres_cluster_df = cadrres_cluster_df[drug_list]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load cluster proportion information"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.387109Z",
     "start_time": "2020-11-27T08:11:46.352471Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>cluster</th>\n",
       "      <th>A1</th>\n",
       "      <th>A2</th>\n",
       "      <th>B1</th>\n",
       "      <th>B2</th>\n",
       "      <th>C1</th>\n",
       "      <th>C2</th>\n",
       "      <th>D1</th>\n",
       "      <th>D2</th>\n",
       "      <th>E1</th>\n",
       "      <th>E2</th>\n",
       "      <th>...</th>\n",
       "      <th>F3</th>\n",
       "      <th>G1</th>\n",
       "      <th>G2</th>\n",
       "      <th>H1</th>\n",
       "      <th>I1</th>\n",
       "      <th>I2</th>\n",
       "      <th>J1</th>\n",
       "      <th>J2</th>\n",
       "      <th>K1</th>\n",
       "      <th>L</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>patient_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HN120</th>\n",
       "      <td>0.01</td>\n",
       "      <td>5.49e-03</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.13e-01</td>\n",
       "      <td>0.18</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.34</td>\n",
       "      <td>0.12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>HN137</th>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00e+00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5.68e-03</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.34</td>\n",
       "      <td>0.09</td>\n",
       "      <td>...</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 23 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "cluster       A1        A2   B1   B2   C1   C2        D1    D2    E1    E2  \\\n",
       "patient_id                                                                   \n",
       "HN120       0.01  5.49e-03  0.0  0.0  0.0  0.0  3.13e-01  0.18  0.00  0.00   \n",
       "HN137       0.00  0.00e+00  0.0  0.0  0.0  0.0  5.68e-03  0.00  0.34  0.09   \n",
       "\n",
       "cluster     ...   F3    G1    G2   H1   I1   I2   J1   J2   K1     L  \n",
       "patient_id  ...                                                       \n",
       "HN120       ...  0.0  0.34  0.12  0.0  0.0  0.0  0.0  0.0  0.0  0.03  \n",
       "HN137       ...  0.1  0.00  0.00  0.0  0.0  0.0  0.0  0.0  0.0  0.01  \n",
       "\n",
       "[2 rows x 23 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "freq_df = pd.read_excel('../data/patient/percent_patient_tpm_cluster.xlsx', index_col=[0, 1]).reset_index()\n",
    "freq_df = freq_df.pivot(index='patient_id', columns='cluster', values='percent').fillna(0) / 100\n",
    "\n",
    "patient_list = freq_df.index\n",
    "\n",
    "freq_df.head(2)\n",
    "#show patient by cluster percentage"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Predict cell death percentage at the `ref_type` dosage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.394342Z",
     "start_time": "2020-11-27T08:11:46.388483Z"
    }
   },
   "outputs": [],
   "source": [
    "pred_delta_df = pd.DataFrame(cadrres_cluster_df.values - drug_info_df[ref_type].values, columns=drug_list, index=cluster_list)\n",
    "pred_cv_df = 100 / (1 + (np.power(2, -pred_delta_df)))\n",
    "pred_kill_df = 100 - pred_cv_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.435634Z",
     "start_time": "2020-11-27T08:11:46.395832Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List of cluster in each patient\n",
      "HN120 ['D1' 'D2' 'G1' 'G2'] [0.31318681 0.17582418 0.34065934 0.12087912]\n",
      "HN137 ['E1' 'E2' 'E3' 'F1' 'F2' 'F3'] [0.34090909 0.08522727 0.07386364 0.26704545 0.11931818 0.09659091]\n",
      "HN148 ['C1' 'C2' 'H1'] [0.31351351 0.20540541 0.45945946]\n",
      "HN159 ['I1' 'I2' 'K1'] [0.31736527 0.18562874 0.48502994]\n",
      "HN160 ['B1' 'B2' 'L'] [0.42222222 0.41481481 0.16296296]\n",
      "HN182 ['J1' 'J2' 'L'] [0.71910112 0.20224719 0.07865169]\n"
     ]
    }
   ],
   "source": [
    "rows = []\n",
    "print('List of cluster in each patient')\n",
    "for p in patient_list:\n",
    "    c_list = freq_df.loc[p][freq_df.loc[p] >= freq_cutoff].index.values\n",
    "    freqs = freq_df.loc[p][freq_df.loc[p] >= freq_cutoff].values\n",
    "\n",
    "    print(p, c_list, freqs)\n",
    "\n",
    "    p_pred_delta_weighted = np.matmul(pred_delta_df.loc[c_list].values.T, freqs)\n",
    "    p_pred_delta_mat = pred_delta_df.loc[c_list].values\n",
    "    \n",
    "    p_pred_kill_weighted = np.matmul(pred_kill_df.loc[c_list].values.T, freqs)\n",
    "    p_pred_kill_mat = pred_kill_df.loc[c_list].values\n",
    "\n",
    "    for d_i, d_id in enumerate(drug_list):\n",
    "        rows += [[p, d_id] + ['|'.join(c_list)] + ['|'.join([\"{:.14}\".format(f) for f in freqs])] + \n",
    "                 ['|'.join([\"{:.14}\".format(f) for f in p_pred_delta_mat[:, d_i]])] + \n",
    "                 [\"{:.14}\".format(p_pred_delta_weighted[d_i])] +\n",
    "                 ['|'.join([\"{:.14}\".format(f) for f in p_pred_kill_mat[:, d_i]])] + \n",
    "                 [\"{:.14}\".format(p_pred_kill_weighted[d_i])]\n",
    "                ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.447323Z",
     "start_time": "2020-11-27T08:11:46.436927Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>drug_id</th>\n",
       "      <th>cluster</th>\n",
       "      <th>cluster_cell_death</th>\n",
       "      <th>cell_death</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1001</td>\n",
       "      <td>D1|D2|G1|G2</td>\n",
       "      <td>34.908073458363|36.560577887311|37.43884047451...</td>\n",
       "      <td>34.593677608186</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1003</td>\n",
       "      <td>D1|D2|G1|G2</td>\n",
       "      <td>33.535917537247|32.254984202963|25.96632734701...</td>\n",
       "      <td>28.505381464623</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1004</td>\n",
       "      <td>D1|D2|G1|G2</td>\n",
       "      <td>29.917363245775|33.263999510979|25.55127940412...</td>\n",
       "      <td>27.086112378265</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1006</td>\n",
       "      <td>D1|D2|G1|G2</td>\n",
       "      <td>36.683397262759|34.738211834014|21.31910722766...</td>\n",
       "      <td>28.084570239709</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>D1|D2|G1|G2</td>\n",
       "      <td>10.768034058802|13.470080993016|5.818301286009...</td>\n",
       "      <td>8.5209921135827</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient drug_id      cluster  \\\n",
       "0   HN120    1001  D1|D2|G1|G2   \n",
       "1   HN120    1003  D1|D2|G1|G2   \n",
       "2   HN120    1004  D1|D2|G1|G2   \n",
       "3   HN120    1006  D1|D2|G1|G2   \n",
       "4   HN120    1007  D1|D2|G1|G2   \n",
       "\n",
       "                                  cluster_cell_death       cell_death  \n",
       "0  34.908073458363|36.560577887311|37.43884047451...  34.593677608186  \n",
       "1  33.535917537247|32.254984202963|25.96632734701...  28.505381464623  \n",
       "2  29.917363245775|33.263999510979|25.55127940412...  27.086112378265  \n",
       "3  36.683397262759|34.738211834014|21.31910722766...  28.084570239709  \n",
       "4  10.768034058802|13.470080993016|5.818301286009...  8.5209921135827  "
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "single_drug_pred_df = pd.DataFrame(rows, columns=['patient', 'drug_id', 'cluster', 'cluster_p', 'cluster_delta', 'delta', 'cluster_cell_death', 'cell_death'])\n",
    "single_drug_pred_df[['patient', 'drug_id', 'cluster', 'cluster_cell_death', 'cell_death']].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.457817Z",
     "start_time": "2020-11-27T08:11:46.448653Z"
    }
   },
   "outputs": [],
   "source": [
    "single_drug_pred_df.to_csv(output_dir + 'pred_drug_kill_{}.csv'.format(model_name), index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Predict patient respose to combinatorial drugs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.463666Z",
     "start_time": "2020-11-27T08:11:46.459537Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Docetaxel',\n",
       " 'Doxorubicin',\n",
       " 'Epothilone B',\n",
       " 'Gefitinib',\n",
       " 'Obatoclax Mesylate',\n",
       " 'PHA-793887',\n",
       " 'PI-103',\n",
       " 'Vorinostat']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tested_drug_list = [1007, 133, 201, 1010, 182, 301, 302, 1012]\n",
    "[drug_id_name_dict[str(d)] for d in tested_drug_list]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.479861Z",
     "start_time": "2020-11-27T08:11:46.465185Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>drug_id</th>\n",
       "      <th>cluster</th>\n",
       "      <th>cluster_p</th>\n",
       "      <th>cluster_delta</th>\n",
       "      <th>delta</th>\n",
       "      <th>cluster_cell_death</th>\n",
       "      <th>cell_death</th>\n",
       "      <th>drug_name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1001</td>\n",
       "      <td>D1|D2|G1|G2</td>\n",
       "      <td>0.31318681318681|0.17582417582418|0.3406593406...</td>\n",
       "      <td>0.89891787646367|0.7950907549512|0.74073149856...</td>\n",
       "      <td>0.76608828181776</td>\n",
       "      <td>34.908073458363|36.560577887311|37.43884047451...</td>\n",
       "      <td>34.593677608186</td>\n",
       "      <td>AICA Ribonucleotide</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1003</td>\n",
       "      <td>D1|D2|G1|G2</td>\n",
       "      <td>0.31318681318681|0.17582417582418|0.3406593406...</td>\n",
       "      <td>0.98686783677045|1.0705926967102|1.51153962438...</td>\n",
       "      <td>1.1697813179586</td>\n",
       "      <td>33.535917537247|32.254984202963|25.96632734701...</td>\n",
       "      <td>28.505381464623</td>\n",
       "      <td>Camptothecin</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1004</td>\n",
       "      <td>D1|D2|G1|G2</td>\n",
       "      <td>0.31318681318681|0.17582417582418|0.3406593406...</td>\n",
       "      <td>1.2280740256921|1.0045035858118|1.542851524626...</td>\n",
       "      <td>1.2676836647428</td>\n",
       "      <td>29.917363245775|33.263999510979|25.55127940412...</td>\n",
       "      <td>27.086112378265</td>\n",
       "      <td>Vinblastine</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1006</td>\n",
       "      <td>D1|D2|G1|G2</td>\n",
       "      <td>0.31318681318681|0.17582417582418|0.3406593406...</td>\n",
       "      <td>0.78745659587898|0.90971502511471|1.8838663034...</td>\n",
       "      <td>1.2245948422257</td>\n",
       "      <td>36.683397262759|34.738211834014|21.31910722766...</td>\n",
       "      <td>28.084570239709</td>\n",
       "      <td>Cytarabine</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>D1|D2|G1|G2</td>\n",
       "      <td>0.31318681318681|0.17582417582418|0.3406593406...</td>\n",
       "      <td>3.0508057471845|2.68344052681|4.0167768343325|...</td>\n",
       "      <td>3.2576613093829</td>\n",
       "      <td>10.768034058802|13.470080993016|5.818301286009...</td>\n",
       "      <td>8.5209921135827</td>\n",
       "      <td>Docetaxel</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient drug_id      cluster  \\\n",
       "0   HN120    1001  D1|D2|G1|G2   \n",
       "1   HN120    1003  D1|D2|G1|G2   \n",
       "2   HN120    1004  D1|D2|G1|G2   \n",
       "3   HN120    1006  D1|D2|G1|G2   \n",
       "4   HN120    1007  D1|D2|G1|G2   \n",
       "\n",
       "                                           cluster_p  \\\n",
       "0  0.31318681318681|0.17582417582418|0.3406593406...   \n",
       "1  0.31318681318681|0.17582417582418|0.3406593406...   \n",
       "2  0.31318681318681|0.17582417582418|0.3406593406...   \n",
       "3  0.31318681318681|0.17582417582418|0.3406593406...   \n",
       "4  0.31318681318681|0.17582417582418|0.3406593406...   \n",
       "\n",
       "                                       cluster_delta             delta  \\\n",
       "0  0.89891787646367|0.7950907549512|0.74073149856...  0.76608828181776   \n",
       "1  0.98686783677045|1.0705926967102|1.51153962438...   1.1697813179586   \n",
       "2  1.2280740256921|1.0045035858118|1.542851524626...   1.2676836647428   \n",
       "3  0.78745659587898|0.90971502511471|1.8838663034...   1.2245948422257   \n",
       "4  3.0508057471845|2.68344052681|4.0167768343325|...   3.2576613093829   \n",
       "\n",
       "                                  cluster_cell_death       cell_death  \\\n",
       "0  34.908073458363|36.560577887311|37.43884047451...  34.593677608186   \n",
       "1  33.535917537247|32.254984202963|25.96632734701...  28.505381464623   \n",
       "2  29.917363245775|33.263999510979|25.55127940412...  27.086112378265   \n",
       "3  36.683397262759|34.738211834014|21.31910722766...  28.084570239709   \n",
       "4  10.768034058802|13.470080993016|5.818301286009...  8.5209921135827   \n",
       "\n",
       "             drug_name  \n",
       "0  AICA Ribonucleotide  \n",
       "1         Camptothecin  \n",
       "2          Vinblastine  \n",
       "3           Cytarabine  \n",
       "4            Docetaxel  "
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "single_drug_pred_df.loc[:, 'drug_id'] = single_drug_pred_df.loc[:, 'drug_id'].values.astype(str)\n",
    "single_drug_pred_df.loc[:, 'drug_name'] = [drug_id_name_dict[d] for d in single_drug_pred_df.loc[:, 'drug_id'].values]\n",
    "patient_list = sorted(list(set(single_drug_pred_df['patient'])))\n",
    "\n",
    "single_drug_pred_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Setup all drug combinations by patient"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.492360Z",
     "start_time": "2020-11-27T08:11:46.482425Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(168, 3)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>201</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>1010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>301</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient     A     B\n",
       "0   HN120  1007   133\n",
       "1   HN120  1007   201\n",
       "2   HN120  1007  1010\n",
       "3   HN120  1007   182\n",
       "4   HN120  1007   301"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_combi_list = []\n",
    "n_drugs = len(tested_drug_list)\n",
    "\n",
    "for p in patient_list:\n",
    "    for x in range(0, n_drugs-1):\n",
    "        for y in range(x+1, n_drugs):\n",
    "            drug_x = str(tested_drug_list[x])\n",
    "            drug_y = str(tested_drug_list[y])\n",
    "\n",
    "            drug_combi_list += [[p, drug_x, drug_y]]\n",
    "\n",
    "drug_combi_df = pd.DataFrame(drug_combi_list, columns=['patient', 'A', 'B'])\n",
    "\n",
    "print (drug_combi_df.shape)\n",
    "drug_combi_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.504072Z",
     "start_time": "2020-11-27T08:11:46.493721Z"
    }
   },
   "outputs": [],
   "source": [
    "merge_df = pd.merge(drug_combi_df, single_drug_pred_df, how='left', left_on=['patient', 'A'], right_on=['patient', 'drug_id'])\n",
    "drug_combi_pred_df = pd.merge(merge_df, single_drug_pred_df[['patient', 'drug_id', 'drug_name', 'cluster_delta', 'delta', 'cluster_cell_death', 'cell_death']], how='left', left_on=['patient', 'B'], right_on=['patient', 'drug_id'], suffixes=['_A', '_B'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.554177Z",
     "start_time": "2020-11-27T08:11:46.505548Z"
    }
   },
   "outputs": [],
   "source": [
    "rows = []\n",
    "for _, data in drug_combi_pred_df.iterrows():\n",
    "    \n",
    "    cluster_p = np.array([float(p) for p in data['cluster_p'].split('|')])\n",
    "    \n",
    "    cluster_kill_A = np.array([float(k) for k in data['cluster_cell_death_A'].split('|')])\n",
    "    cluster_kill_B = np.array([float(k) for k in data['cluster_cell_death_B'].split('|')])\n",
    "    \n",
    "    kill_A = float(data['cell_death_A'])\n",
    "    kill_B = float(data['cell_death_B'])\n",
    "    \n",
    "    cluster_kill_C = cluster_kill_A + cluster_kill_B - np.multiply(cluster_kill_A/100, cluster_kill_B/100)*100\n",
    "    kill_C = np.sum(cluster_p * cluster_kill_C)\n",
    "    \n",
    "    best_kill = np.max([kill_A, kill_B])\n",
    "    improve = kill_C - best_kill\n",
    "    improve_p = (kill_C - best_kill) / best_kill\n",
    "    \n",
    "    ##### specificity (entropy) #####\n",
    "    \n",
    "    temp_A = np.sum(cluster_p[cluster_kill_A > cluster_kill_B])\n",
    "    temp_B = np.sum(cluster_p[cluster_kill_A <= cluster_kill_B])\n",
    "    if temp_A == 0 or temp_B == 0:\n",
    "        entropy = 0\n",
    "    else:\n",
    "        entropy = -(temp_A * np.log2(temp_A) + temp_B * np.log2(temp_B))\n",
    "    \n",
    "    sum_kill_dif = np.sum(np.abs(cluster_kill_A - cluster_kill_B))\n",
    "    \n",
    "    ##### save output #####\n",
    "    \n",
    "    rows += [['|'.join([\"{:.14}\".format(k) for k in cluster_kill_C])] + [kill_C, improve, improve_p, entropy, sum_kill_dif]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.561797Z",
     "start_time": "2020-11-27T08:11:46.555447Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(168, 23)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_combi_pred_df = pd.concat([drug_combi_pred_df, pd.DataFrame(rows, columns=['cluster_cell_death_combi', 'cell_death_combi', 'improve', 'improve_p', 'kill_entropy', 'sum_kill_dif'])], axis=1)\n",
    "drug_combi_pred_df.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Final drug combination predictions for patients"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:13:58.616496Z",
     "start_time": "2020-11-27T08:13:58.603623Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>drug_id_A</th>\n",
       "      <th>drug_id_B</th>\n",
       "      <th>cluster</th>\n",
       "      <th>cell_death_A</th>\n",
       "      <th>cell_death_B</th>\n",
       "      <th>cell_death_combi</th>\n",
       "      <th>improve</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>133</td>\n",
       "      <td>D1|D2|G1|G2</td>\n",
       "      <td>8.5209921135827</td>\n",
       "      <td>75.63044860423</td>\n",
       "      <td>77.19</td>\n",
       "      <td>1.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>201</td>\n",
       "      <td>D1|D2|G1|G2</td>\n",
       "      <td>8.5209921135827</td>\n",
       "      <td>60.390313965552</td>\n",
       "      <td>63.04</td>\n",
       "      <td>2.65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>1010</td>\n",
       "      <td>D1|D2|G1|G2</td>\n",
       "      <td>8.5209921135827</td>\n",
       "      <td>15.312538173024</td>\n",
       "      <td>22.39</td>\n",
       "      <td>7.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>182</td>\n",
       "      <td>D1|D2|G1|G2</td>\n",
       "      <td>8.5209921135827</td>\n",
       "      <td>64.658542403814</td>\n",
       "      <td>67.22</td>\n",
       "      <td>2.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>301</td>\n",
       "      <td>D1|D2|G1|G2</td>\n",
       "      <td>8.5209921135827</td>\n",
       "      <td>63.393584136265</td>\n",
       "      <td>66.39</td>\n",
       "      <td>3.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient drug_id_A drug_id_B      cluster     cell_death_A     cell_death_B  \\\n",
       "0   HN120      1007       133  D1|D2|G1|G2  8.5209921135827   75.63044860423   \n",
       "1   HN120      1007       201  D1|D2|G1|G2  8.5209921135827  60.390313965552   \n",
       "2   HN120      1007      1010  D1|D2|G1|G2  8.5209921135827  15.312538173024   \n",
       "3   HN120      1007       182  D1|D2|G1|G2  8.5209921135827  64.658542403814   \n",
       "4   HN120      1007       301  D1|D2|G1|G2  8.5209921135827  63.393584136265   \n",
       "\n",
       "   cell_death_combi  improve  \n",
       "0             77.19     1.56  \n",
       "1             63.04     2.65  \n",
       "2             22.39     7.08  \n",
       "3             67.22     2.56  \n",
       "4             66.39     3.00  "
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_combi_pred_df = drug_combi_pred_df[['patient', 'drug_id_A', 'drug_name_A', 'drug_id_B', 'drug_name_B', 'cluster', 'cluster_p', 'cluster_cell_death_A', 'cluster_cell_death_B', 'cluster_cell_death_combi', 'cell_death_A', 'cell_death_B', 'cell_death_combi', 'improve', 'improve_p', 'kill_entropy', 'sum_kill_dif']]\n",
    "\n",
    "drug_combi_pred_df[['patient', 'drug_id_A', 'drug_id_B', 'cluster', 'cell_death_A', 'cell_death_B', 'cell_death_combi', 'improve']].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-27T08:11:46.590117Z",
     "start_time": "2020-11-27T08:11:46.581987Z"
    }
   },
   "outputs": [],
   "source": [
    "drug_combi_pred_df.to_csv(output_dir + 'pred_combi_kill_{}.csv'.format(ref_type, model_name), index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "**Authors:** [Chayaporn Suphavilai](mailto:@.com), [Rafael Peres da Silva](), Genome Institute of Singapore, Nagarajan Lab, November 2020\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Reproducibility tips from https://github.com/jupyter-guide/ten-rules-jupyter"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
